{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a4fc784-97e7-4e98-8afc-cf2da0892a2a",
   "metadata": {},
   "outputs": [],
   "source": [
    "solution([-10, -9, -8, -6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]);\n",
    "// returns \"-10--8,-6,-3-1,3-5,7-11,14,15,17-20\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ee6ad246-289a-4f90-a0b8-7579aff0033e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def xx1(lst, t):\n",
    "    b = 1\n",
    "    for i in range(t, len(lst)-1):\n",
    "        if lst[i] == lst[i+1] - 1:\n",
    "            b += 1\n",
    "        else:\n",
    "            break\n",
    "    if b == 1:\n",
    "        return (1, str(lst[t]) + ',')\n",
    "    elif b == 2:\n",
    "        return (2, str(lst[t]) + ',' + str(lst[t+1]) + ',')\n",
    "    return (b, str(lst[t]) + '-' + str(lst[t+b-1]) + ',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "16890705-84e6-4fd4-a564-8670c84256be",
   "metadata": {},
   "outputs": [],
   "source": [
    "def solution(lst):\n",
    "    res, t = '', 0\n",
    "    while t < len(lst):\n",
    "        n, s = xx1(lst, t)\n",
    "        res += s\n",
    "        t += n\n",
    "    return res[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "81fa7c50-057a-44fb-bf74-f2e804fc02dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'-10--8,-6,-3-1,3-5,7-11,14,15,17-20'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solution([-10, -9, -8, -6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35127f74-8f37-4471-936e-f1028f54a0e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "\"-10--8,-6,-3-1,3-5,7-11,14,15,17-20\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a85999a-80ae-43fe-82f9-56f898804008",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45aed5b4-0ab2-4590-9a50-8e88c2dd0e6c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1108f70c-75d3-4ef7-85e8-796ecd726335",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a, b = (1, 2)\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "88bd51b1-89a5-4ab4-8f2a-02aac2daf2da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = (1,2)[1]\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73f4c502-e816-43de-b5b2-03fab365d12f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b843d29d-3e93-4e38-8be8-24503ad30ded",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b65cc3e8-e683-468d-86eb-5c7104582e7d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def solution(args):\n",
    "    out = []\n",
    "    beg = end = args[0]\n",
    "    \n",
    "    for n in args[1:] + [\"\"]:        \n",
    "        if n != end + 1:\n",
    "            if end == beg:\n",
    "                out.append( str(beg) )\n",
    "            elif end == beg + 1:\n",
    "                out.extend( [str(beg), str(end)] )\n",
    "            else:\n",
    "                out.append( str(beg) + \"-\" + str(end) )\n",
    "            beg = n\n",
    "        end = n\n",
    "    \n",
    "    return \",\".join(out)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "458065f8-c39e-48e3-81ab-d526ca616b69",
   "metadata": {},
   "outputs": [],
   "source": [
    "def solution(arr):\n",
    "    ranges = []\n",
    "    a = b = arr[0]\n",
    "    for n in arr[1:] + [None]:\n",
    "        if n != b+1:\n",
    "            ranges.append(str(a) if a == b else \"{}{}{}\".format(a, \",\" if a+1 == b else \"-\", b))\n",
    "            a = n\n",
    "        b = n\n",
    "    return \",\".join(ranges)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bed56cac-2948-480c-a581-2f61e1d05230",
   "metadata": {},
   "outputs": [],
   "source": [
    "def solution(args):\n",
    "    output = ''\n",
    "    for n in args:                                                                              # In comments: 'group' = any individual integer, pair, or range of 3+\n",
    "        if n == max(args): output += str(n)                                                     # At the end of the list, there's no extra punctuation\n",
    "        elif n+1 not in args or (n-1 not in args and n+2 not in args): output += str(n) + ','   # You get a comma at the end of a group, or in the middle of a pair\n",
    "        elif n-1 not in args: output += str(n) + '-'                                            # You get a dash if you're at the start of a group and didn't get a comma\n",
    "    return output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5902a16-7f69-434f-93b8-b486a029017e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from itertools import groupby\n",
    "def solution(args):\n",
    "    grps = ([v[1] for v in g] for _,g in groupby(enumerate(args), lambda p: p[1]-p[0]))\n",
    "    return ','.join('{}{}{}'.format(g[0],'-'if len(g)>2 else',',g[-1])\n",
    "        if len(g)>1 else str(g[0]) for g in grps)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "946ee346-0e68-4b38-952f-6a634383e6f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def solution(nums):\n",
    "    nums.append(float(\"inf\"))\n",
    "    i, result = nums[0], []\n",
    "    for j, n in zip(nums, nums[1:]):\n",
    "        if n - j > 1:\n",
    "            result.append(str(i) if i == j else f\"{i}{'-' if j - i > 1 else ','}{j}\")\n",
    "            i = n\n",
    "    return \",\".join(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5cde50d6-697e-425a-9fc4-543b37bd7917",
   "metadata": {},
   "outputs": [],
   "source": [
    "from itertools import groupby\n",
    "def solution(args):\n",
    "    d=''\n",
    "    fun = lambda x: x[1] - x[0]\n",
    "    for k, g in groupby(enumerate(args), fun):\n",
    "        c=[b for a,b in g]\n",
    "        if len(c)==1:\n",
    "            d=d+'%d,'%c[0]\n",
    "        elif len(c)==2:\n",
    "            d=d+'%d,%d,'%(c[0],c[-1])\n",
    "        else:\n",
    "            d=d+'%d-%d,'%(c[0],c[-1])\n",
    "    return (d[:-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a5aa279-624b-4895-ba04-6c7bf6d7723e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80a9f89d-c797-445d-9225-bba758216171",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3a95f1db-eaba-4a2e-9954-73cf353845cb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e86fc1b-5022-410e-b514-407239b582c3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "773ba9e8-1596-4a3c-94e4-3a0b481c4d8a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50ae205c-9d92-4135-9258-38b4c5fe852e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
